/*
 * Copyright (C) 2024 HiHope Open Source Organization.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import ConfigData from '../../../../../../../common/utils/src/main/ets/default/baseUtil/ConfigData';
import type ISettingsController
  from '../../../../../../../common/component/src/main/ets/default/controller/ISettingsController';
import SwitchController from '../../../../../../../common/component/src/main/ets/default/controller/SwitchController';
import LogUtil from '../../../../../../../common/utils/src/main/ets/default/baseUtil/LogUtil';
import { LogAll } from '../../../../../../../common/utils/src/main/ets/default/baseUtil/LogDecorator';
import { GlobalContext } from '../../../../../../../common/utils/src/main/ets/default/baseUtil/GlobalContext';
import common from '@ohos.app.ability.common';
import settings from '@ohos.settings';

const MODULE_TAG = ConfigData.TAG + '.miracastController -> ';
const MIRACAST_QUICK_PLAY_KEY = 'settings.wifi.miracast.quickplay';
const FALSE = "0";
const TRUE = "1";

@LogAll
export default class MiracastController extends SwitchController {
  /**
   * Initialize data.
   */
  initData(): ISettingsController {
    super.initData();
    let context = GlobalContext.getContext().getObject(GlobalContext.globalKeySettingsAbilityContext) as common.Context;
    this.isOn = settings.getValueSync(context, MIRACAST_QUICK_PLAY_KEY, FALSE) === TRUE;
    return this;
  }

  /**
   * After current value changed event
   */
  afterCurrentValueChanged(): void {
    LogUtil.info(MODULE_TAG + 'isOn' + this.isOn);
    let context = GlobalContext.getContext().getObject(GlobalContext.globalKeySettingsAbilityContext) as common.Context;
    if (this.isOn) {
      settings.setValue(context, MIRACAST_QUICK_PLAY_KEY, TRUE);
    } else {
      settings.setValue(context, MIRACAST_QUICK_PLAY_KEY, FALSE);
    }
  }
}